package com.fm.parser.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.joda.time.DateTime;

/**
 * @author final
 */
public class Json {
    protected SerializeConfig serializeConfig = new SerializeConfig();
    protected ParserConfig parserConfig = new ParserConfig();

    public void addCodec(FastJsonCodec codec) {
        serializeConfig.put(DateTime.class, codec);
        parserConfig.putDeserializer(DateTime.class, codec);
    }

    public String encode(Object object, SerializerFeature... serializerFeatures) {
        return JSON.toJSONString(object, serializeConfig, serializerFeatures);
    }

    public <T> T decode(String json, Class<T> clazz) {
        return JSON.parseObject(json, clazz, parserConfig);
    }
}
